const db = require('../../lib/conn_oracle')

exports.getStations = async (req,res) => {
    let resDatas
    let status
    let msg
    let sql = `select location,replace(description,'站','') as description
               from c##wxw.location
               where cust_linenum = 'L3B'
                 and areatype <> '线路'
                 and description not like '%嘉禾%'
                 and description not like '%主变%'
                 and description not like '%风亭%'
               order by location`
    try{
        let stations = await db.search2(sql)
        status =  stations.errorNum ? 0 : 1
        msg = stations.errorNum? "查询失败" : "查询成功"
        resDatas = {stations}
        res.json({
            status,
            msg,
            resDatas
        })
    }catch(err){
        res.json({
            status:0,
            msg:"查询失败",
            resDatas:{err}
        })
    }
}

exports.getBasData = async (req,res) => {
    console.log(req.body)
    let {begindate,enddate,station} = req.body
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let reqDatas = [station,begindate,enddate]
    let sql = `
            select *
            from c##wxw.bas_equipment_status b
            where b.station = :station
              and b.date_time >= to_timestamp(:begindate,'yyyy-mm-dd hh24:mi:ss.ff')
              and b.date_time <= to_timestamp(:enddate,'yyyy-mm-dd hh24:mi:ss.ff')
            order by b.bdate,b.btime,b.station,b.ename desc 
            `
    try{
        let basDataInfo = await db.search(sql,reqDatas)
        resStatus =  basDataInfo.errorNum ? 0 : 1
        resMsg = basDataInfo.errorNum? "查询失败" : "查询成功"
        resDatas = {basDataInfo}
        res.json({
            resStatus,
            resMsg,
            resDatas
        })
    }catch(err){
        res.json({
            resStatus:0,
            resMsg:"查询失败",
            resDatas:{err}
        })
    }
}

//编辑BAS数据
exports.editBasData = async (req,res)=>{
    let {type,form} = req.body
    console.log(type)
    console.log(form)
    let {ENAME,DESCRIPTION,STATION,DATE_TIME} = form
    let msgType = ""
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let reqDatas = []
    let editSql
    //新增或更新
    if(type === "add" || type === "copy"){
        msgType = "新增"
        reqDatas = [form.BDATE,form.BTIME,form.CNAME,form.ENAME,form.DESCRIPTION,form.VALUE,form.UNIT,form.STATION,form.DATE_TIME]
        editSql = `
            insert into c##wxw.bas_equipment_status (bdate,btime,cname,ename,description,value,unit,station,date_time)
            values (:BDATE,:BTIME,:CNAME,:ENAME,:DESCRIPTION,:VALUE,:UNIT,:STATION,to_timestamp(:DATE_TIME,'yyyy-mm-dd hh24:mi:ss.ff'))
        `
    }
    if(type === "delete"){
        msgType = "删除"
        reqDatas = [ENAME,DESCRIPTION,STATION,DATE_TIME]
        editSql = `
            delete from c##wxw.bas_equipment_status
            where ename = :ENAME
              and description = :DESCRIPTION
              and station = :STATION
              and date_time = to_timestamp(:DATE_TIME,'yyyy-mm-dd hh24:mi:ss.ff')
        `
    }
    try{
        let result = await db.writeTab(editSql,reqDatas)
        resStatus =  result.errorNum ? 0 : 1
        resMsg = result.errorNum? `${msgType}失败` : `${msgType}成功`
        resDatas = {result}
        res.json({
            resStatus,
            resMsg,
            resDatas
        })
    }catch(err){
        res.json({
            resStatus:0,
            resMsg:`${msgType}失败`,
            resDatas:{err}
        })
    }

}


// 批量编辑基础数据
exports.batchEditBasData = async (req,res) => {
    let { type,form } = req.body  
    console.log(type)
    console.log(form)
    let msgType = ""
    let resMsg = ""
    let resStatus = 0
    let resDatas = {}
    let editSql
    let reqDatas = []
    let options = {
        autoCommit:true,
    }
    //表无唯一的ID字段，只能新增
    if(type === "batchAdd" || type === "batchUpdate"){
        msgType = "批量新增"
        reqDatas = form
        editSql = `
            insert into c##wxw.bas_equipment_status (bdate,btime,cname,ename,description,value,unit,station,date_time)
            values (:BDATE,:BTIME,:CNAME,:ENAME,:DESCRIPTION,:VALUE,:UNIT,:STATION,to_timestamp(:DATE_TIME,'yyyy-mm-dd hh24:mi:ss.ff'))
        `
    }
    if(type === "batchDel"){
        msgType = "批量删除"
        form.forEach(item => {
            let row = [item.ENAME,item.DESCRIPTION,item.STATION,item.DATE_TIME]
            reqDatas.push(row)
        })
        editSql = `
            delete from c##wxw.bas_equipment_status
            where ename = :ENAME
              and description = :DESCRIPTION
              and station = :STATION
              and date_time = to_timestamp(:DATE_TIME,'yyyy-mm-dd hh24:mi:ss.ff')
        `
    }
    try{
        let result = await db.writeManyTab(editSql,reqDatas,options)
        resStatus =  result.errorNum ? 0 : 1
        resMsg = result.errorNum? `${msgType}失败` : `${msgType}成功`
        resDatas = {result}
        res.json({
            resStatus,
            resMsg,
            resDatas
        })
    }catch(err){
        res.json({
            resStatus:0,
            resMsg:`${msgType}失败`,
            resDatas:{err}
        })
    }
}
